"""
Case Type   : Log_Max_Size
Case Name   : 设置数据库/用户/会话级别参数，合理报错
Create At   : 2024/10/26
Owner       : liu-tong-8848
Description :
              1.查询log_max_size默认值
              2.设置数据库级别
              3.设置用户级别
              4.设置会话级别
              5.恢复log_max_size默认值
Expect      :
              1.默认值为0
              2.设置失败，合理报错
              3.设置失败，合理报错
              4.设置失败，合理报错
              5.成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.db_user_node = Node(node='PrimaryDbUser')

    def test_add_dimension(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        step1 = '--step1:查询log_max_size默认值;expect:成功'
        self.log.info(step1)
        sql = "show log_max_size;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("0", result, "执行失败" + step1)
        self.assertNotIn("ERROR", result, "执行失败" + step1)

        step2 = '--step2:设置数据库级别，log_max_size=10MB;expect:合理报错'
        self.log.info(step2)
        create_db_sql = f"drop database if exists db_log_case0008; create database db_log_case0008;"
        self.log.info(create_db_sql)
        create_db_result = self.primary_sh.execut_db_sql(create_db_sql)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, create_db_result, '执行失败:' + step2)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_db_result, '执行失败:' + step2)
        alter_db_sql = f"alter database db_log_case0008 set log_max_size to ‘10MB’;"
        self.log.info(alter_db_sql)
        alter_db_result = self.primary_sh.execut_db_sql(alter_db_sql)
        self.assertIn("ERROR:  parameter \"log_max_size\" cannot be changed now", alter_db_result, '执行失败:' + step2)
        sql = "show log_max_size;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("0", result, "执行失败" + step2)
        self.assertNotIn("ERROR", result, "执行失败" + step2)

        step3 = '--step3:设置用户级别，log_max_size=10MB;expect:合理报错'
        create_usr_sql = f"drop user if exists u_log_case0008; create user u_log_case0008 with password 'test@123';"
        self.log.info(create_usr_sql)
        create_usr_result = self.primary_sh.execut_db_sql(create_usr_sql)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, create_usr_result, '执行失败:' + step3)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, create_usr_result, '执行失败:' + step3)
        alter_usr_sql = f"alter user u_log_case0008 set log_max_size to '10MB';"
        self.log.info(alter_usr_sql)
        alter_usr_result = self.primary_sh.execut_db_sql(alter_usr_sql)
        self.assertIn("ERROR", alter_usr_result, '执行失败:' + step3)
        self.assertIn("cannot be changed now", alter_usr_result, '执行失败:' + step3)
        sql = "show log_max_size;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("0", result, "执行失败" + step3)
        self.assertNotIn("ERROR", result, "执行失败" + step3)

        step4 = '--step4:设置会话级别，log_max_size=10MB;expect:合理报错'
        self.log.info(step4)
        alter_system_set_param_sql = f"set log_max_size to '10MB';show log_max_size;"
        result = self.primary_sh.execut_db_sql(alter_system_set_param_sql)
        self.log.info(result)
        self.assertIn("ERROR", result, '执行失败:' + step4)
        self.assertIn("cannot be changed now", result, '执行失败:' + step4)
        self.assertIn("0", result, "执行失败" + step4)

    def tearDown(self):
        step5 = '--step5:恢复log_max_size默认值;expect:成功'
        self.log.info(step5)
        result = self.primary_sh.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"log_max_size=0")
        self.assertTrue(result, '执行失败:' + step5)
        self.primary_sh.restart_db_cluster()
        alter_para = self.com.show_param("log_max_size")
        self.assertEqual(alter_para, '0', '执行失败:' + step5)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
